home *** CD-ROM | disk | FTP | other *** search
/ The CICA Windows Explosion! / The CICA Windows Explosion! - Disc 2.iso / misc / gs261src.zip / gdevsco.c < prev    next >
C/C++ Source or Header  |  1993-05-13  |  6KB  |  229 lines

  1. /* Copyright (C) 1989, 1992, 1993 Aladdin Enterprises.  All rights reserved.
  2.  
  3. This file is part of Ghostscript.
  4.  
  5. Ghostscript is distributed in the hope that it will be useful, but
  6. WITHOUT ANY WARRANTY.  No author or distributor accepts responsibility
  7. to anyone for the consequences of using it or for whether it serves any
  8. particular purpose or works at all, unless he says so in writing.  Refer
  9. to the Ghostscript General Public License for full details.
  10.  
  11. Everyone is granted permission to copy, modify and redistribute
  12. Ghostscript, but only under the conditions described in the Ghostscript
  13. General Public License.  A copy of this license is supposed to have been
  14. given to you along with Ghostscript so you can know your rights and
  15. responsibilities.  It should be in a file named COPYING.  Among other
  16. things, the copyright notice and this notice must be preserved on all
  17. copies.  */
  18.  
  19. /* gdevsco.c - 17Jul91 - wb - based on gdevpcfb.c */
  20. /* 31Jul91 - Rick Calder rick@rick.att.com - ifdefs for AT&T UNIX 4.0 2.1 */
  21. /* 13Sep91 - wb - modify for gs24b2 */
  22. /*  9Mar92 - wb - modify for gs24b4 */
  23. /* generate SCO Xenix/Unix style memory mapped ioctl output */
  24. #include "memory_.h"
  25. #include "gx.h"
  26. #include "gserrors.h"
  27. #include "gxdevice.h"
  28. #include "gdevpcfb.h"
  29. #include <signal.h>
  30.  
  31. #ifdef M_XENIX
  32. #include <sys/console.h>        /* SCO Xenix and SCO UNIX */
  33. #else
  34. #include <sys/kd.h>            /* AT&T SVR4 */
  35. #endif
  36.  
  37. #ifndef CONSIO
  38. #include <sys/machdep.h>        /* Xenix needs this also */
  39. #endif
  40.  
  41. private int console_fd = -1;    /* file descriptor of console */
  42. fb_ptr fb_addr;            /* address of frame buffer for unix */
  43. private int cur_mode = -1;    /* current video mode */
  44.  
  45. /* open the console */
  46. /* possible files to open:
  47.  * /dev/console = current system console
  48.  * /dev/vga = vga monitor
  49.  * /dev/tty = current terminal
  50.  */
  51. private void
  52. open_console()
  53. {
  54.     char *dev;
  55.     char *getenv();
  56.     if (console_fd != -1) return;
  57.     dev = getenv("GSDEVICE");
  58.     if (dev == NULL || *dev == '\0')
  59.         dev = "/dev/tty";
  60.     console_fd = open(dev, 0);
  61.     if (console_fd == -1) {
  62.         ega_close((gx_device *)NULL);
  63.         eprintf1("unable to map display '%s'\n", dev);
  64.         perror("open_console");
  65.         exit(1);
  66.     }
  67. }
  68.  
  69. /* Output to a port */
  70. void
  71. outportb(uint port, byte data)
  72. {
  73.     int i;
  74.     struct port_io_arg pio;
  75.  
  76.     if (console_fd == -1) open_console();
  77.     pio.args[0].dir = OUT_ON_PORT;
  78.     pio.args[0].port = port;
  79.     pio.args[0].data = data;
  80.     pio.args[1].port = 0;
  81.     pio.args[2].port = 0;
  82.     pio.args[3].port = 0;
  83.     i = ioctl(console_fd, CONSIO, (long)(&pio));
  84.     if (i == -1) {
  85.         ega_close((gx_device *)NULL);
  86.         eprintf("error setting device register\n");
  87.         perror("outportb");
  88.         exit(1);
  89.     }
  90. }
  91.  
  92. /* Output to 2 consecutive ports */
  93. void
  94. outport2(uint port, byte index, byte data)
  95. {
  96.     int i;
  97.     struct port_io_arg pio;
  98.  
  99.     if (console_fd == -1) open_console();
  100.     pio.args[0].dir = OUT_ON_PORT;
  101.     pio.args[0].port = port;
  102.     pio.args[0].data = index;
  103.     pio.args[1].dir = OUT_ON_PORT;
  104.     pio.args[1].port = port + 1;
  105.     pio.args[1].data = data;
  106.     pio.args[2].port = 0;
  107.     pio.args[3].port = 0;
  108.     i = ioctl(console_fd, CONSIO, (long)(&pio));
  109.     if (i == -1) {
  110.         ega_close((gx_device *)NULL);
  111.         eprintf("error setting device register\n");
  112.         perror("outport2");
  113.         exit(1);
  114.     }
  115. }
  116.  
  117. /* interrupt signal handler */
  118. /*  restore the video mode and exit */
  119. private void
  120. ega_int_handler(int sig)
  121. {
  122.     ega_close((gx_device *)NULL);
  123.     eprintf("GS exiting...\n");
  124.     exit(1);
  125. }
  126.  
  127. /*
  128.  * FIXME to make this work, the SIGCONT handler must restore the
  129.  * the video state, including all the registers.
  130.  * For now, I made the SIGSTOP handler exit just call the SIGINT handler
  131.  */
  132.  
  133. #ifdef    SIGTSTP
  134. /* user tried to stop us.  restore video and stop */
  135. private void
  136. ega_tstp_handler(int sig)
  137. {
  138. #if 1
  139.     ega_int_handler(sig);
  140. #else
  141.     /* Preferable, but sco does not restore the monitor corretly */
  142.     signal(SIGTSTP, ega_tstp_handler);
  143.     ega_close((gx_device *)NULL);
  144.     eprintf("GS stopping...\n");
  145.     signal(SIGSTOP,SIG_DFL);
  146.     kill(getpid(), SIGSTOP);
  147. #endif
  148. }
  149. #endif    /* SIGTSTP */
  150.  
  151. #ifdef    SIGCONT
  152. /* we were unstopped.  reopen video */
  153. private void
  154. ega_cont_handler(int sig)
  155. {
  156. #if 1
  157.     ega_int_handler(sig);
  158. #else
  159.     signal(SIGCONT, ega_cont_handler);
  160.     ega_set_mode(cur_mode);
  161. #endif
  162. }
  163. #endif    /* SIGCONT */
  164.  
  165. /* ------ Internal routines ------ */
  166.  
  167. /* Catch signals so we can restore the video mode on exit. */
  168. void
  169. ega_set_signals(gx_device *dev)
  170. {    signal(SIGINT, ega_int_handler);
  171.     signal(SIGTERM, ega_int_handler);
  172. #ifdef    SIGTSTP
  173.     signal(SIGTSTP, ega_tstp_handler);
  174. #endif
  175. #ifdef    SIGCONT
  176.     signal(SIGCONT, ega_cont_handler);
  177. #endif
  178. }
  179.  
  180. /* Read the device mode */
  181. int
  182. ega_get_mode(void)
  183. {    int mode;
  184.     open_console();
  185.     mode = ioctl(console_fd, CONS_CURRENT, 0L);
  186.     if (mode == -1) {
  187.         ega_close((gx_device *)NULL);
  188.         eprintf("unable to get current console mode\n");
  189.         perror("ega_get_mode");
  190.         exit(1);
  191.     }
  192.     if (mode == M_ENH_CG640 || mode == M_CG640x350) return(0x10);
  193.     if (mode == M_VGA12) return(0x12);
  194.     return(0x03);
  195. }
  196.  
  197. /* Set the device mode */
  198. void
  199. ega_set_mode(int mode)
  200. {    int i, mode1;
  201.     open_console();
  202.     cur_mode = mode;
  203.     mode1 = -1;
  204.     if (mode == 0x10) mode = SW_ENH_CG640;
  205.     else if (mode == 0x12) mode = SW_VGA12;
  206.     else if (mode == 0x03) { mode = SW_VGA80x25; mode1 = SW_ENHC80x25; }
  207.     else {
  208.         eprintf1("can not set to video mode %d\n", mode);
  209.         exit(1);
  210.     }
  211.     i = ioctl(console_fd, mode, 0L);
  212.     if (i == -1 && mode1 != -1)
  213.         i = ioctl(console_fd, mode1, 0L);
  214.     if (i == -1) {
  215.         ega_close((gx_device *)NULL);
  216.         eprintf("unable to set console mode\n");
  217.         perror("ega_set_mode");
  218.         exit(1);
  219.     }
  220.     i = ioctl(console_fd, MAPCONS, 0L);
  221.     if (i == -1) {
  222.         ega_close((gx_device *)NULL);
  223.         eprintf("unable to map console adaptor's display memory\n");
  224.         perror("ega_set_mode");
  225.         exit(1);
  226.     }
  227.     fb_addr = (fb_ptr) (i);
  228. }
  229.